package com.neteast.androidclient.newscenter.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class DeBugUtil {
	
	public static void printException(Context context,Exception e){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时");
			SimpleDateFormat format2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
			Date date = new Date(System.currentTimeMillis());
			String fileName = format.format(date);
			String dividerLine="\n\n\n\n====================="+format2.format(date)+"=====================\n";
			
			File logFile=new File(createDir(context), "newscenter/log/"+fileName+".txt");
			logFile.getParentFile().mkdirs();
			logFile.createNewFile();
			FileWriter writer=new FileWriter(logFile,true);
			
			StringBuilder sb=new StringBuilder(dividerLine);
			sb.append(e.toString()).append("\n");
			StackTraceElement[] stackTrace = e.getStackTrace();
			final int N=stackTrace.length;
			for (int i=0;i<N;i++) {
				sb.append(stackTrace[i].toString()).append("\n");
			}
			writer.write(sb.toString());
			writer.close();
		} catch (IOException e2) {
			Log.e("test", e2.getMessage());
		}
	}
	
	private static String createDir(Context context) {
		String dir=null;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			dir=Environment.getExternalStorageDirectory().getAbsolutePath();
		}else {
			dir=context.getFilesDir().toString();
		}
		return dir;
	}
}
